//
//  XDWebInfoDetailViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-5-29.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDWebInfoDetailViewController.h"
#import "WebInfo_GetDetail_Task.h"
#import "XDAttachCell.h"
#import "WebInfoAttachModel.h"
#import "CommonUtil.h"
#import "AttachDownloadTask.h"

@interface XDWebInfoDetailViewController ()
{
    NSString *getDetailTaskGuid;
    NSMutableArray *AttachList;
    int selectedIndex;
    NSString *infoguid;
    NSString *downloadtaskguid;
}
@end

@implementation XDWebInfoDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"信息详情";
    infoguid = [intentParams objectForKey:@"InfoGuid"];
    NSMutableDictionary *params = self.getPassDictionary;
    [params setObject:infoguid forKey:@"InfoGuid"];
    [self showMBPHUD];
    getDetailTaskGuid = [[[WebInfo_GetDetail_Task alloc]initWithContext:self andParams:params]startTask];
    
    [self.tabBar setSelectedItem:self.tbiNr];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_wv release];
    [_tabBar release];
    [_tabBar release];
    [_tbiNr release];
    [_tbiFj release];
    [_tvFj release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setWv:nil];
    [self setTabBar:nil];
    [self setTabBar:nil];
    [self setTbiNr:nil];
    [self setTbiFj:nil];
    [self setTvFj:nil];
    [super viewDidUnload];
}

#pragma bizlogic

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    [self hideMBPHUD];
    if ([getDetailTaskGuid isEqualToString:threadGuid]) {
        NSString *bs = (NSString *)obj;
        NSString *displayname = [intentParams objectForKey:@"PostUserName"];
        NSString *date = [intentParams objectForKey:@"PostDate"];
        NSString *txtTitle = [intentParams objectForKey:@"txtTitle"];
        NSString *content = [StringUtil getXMLFormatedAttribute:bs :@"content"];
        content = [[content stringByReplacingOccurrencesOfString:@"<![CDATA[" withString:@""]stringByReplacingOccurrencesOfString:@"]]>" withString:@""];
        NSString *infoType = [StringUtil getXMLFormatedAttribute:bs :@"infoProperty"];
        
        if ([infoType.lowercaseString isEqualToString:@"news"]||[infoType.lowercaseString isEqualToString:@"attach"]) {
            NSString *disContent = [NSString stringWithFormat:@"%@<div id='oschina_title'>%@</div><div id='oschina_outline'>%@ %@</div><hr />%@",HTMLSTYPE1,txtTitle,displayname,[[date componentsSeparatedByString:@" "] objectAtIndex:0],content];
            [self.wv loadHTMLString:disContent baseURL:nil];
            
            AttachList = [[WebInfo_GetDetail_Task getAttackList:bs]retain];
            if (AttachList.count>0) {
                self.tbiFj.badgeValue = [NSString stringWithFormat:@"%i",AttachList.count];
            }
            
        }
        else if ([infoType.lowercaseString isEqualToString:@"link"])
        {
            NSURL *url = [NSURL URLWithString:[StringUtil getXMLFormatedAttribute:bs :@"LinkUrl"]];
            NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
                                                      cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                  timeoutInterval:60.0];
            [self.wv loadRequest:theRequest];
        }
    }
    else if ([downloadtaskguid isEqualToString:threadGuid])
    {
        NSString *path = (NSString *)obj;
        [self openFileFromSecondApp:path];
    }
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if (item.tag==2001) {
        self.wv.hidden = NO;
        self.tvFj.hidden = YES;
        selectedIndex = 0;
    }
    else if (item.tag==2002) {
        self.wv.hidden = YES;
        self.tvFj.hidden = NO;
        selectedIndex = 1;
        [self.tvFj reloadData];
    }
}

#pragma tableview

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedIndex==1) {
        static NSString *identifier = @"XDFlowInfoCell";
        XDAttachCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"XDAttachCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        
        WebInfoAttachModel *m = [AttachList objectAtIndex:indexPath.row];
        cell.lbAttachName.text = m.AttFileName;
        return cell;
    }
    return nil;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return AttachList.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedIndex==1) {
        WebInfoAttachModel *m = [AttachList objectAtIndex:indexPath.row];
        if ([m.isBigFile isEqualToString:@"1"]) {
            [UITool toast:@"大附件无法下载！"];
            return;
        }
        
        NSString *savePath = [NSString stringWithFormat:@"%@/Documents/attaches/%@/%@",NSHomeDirectory(),infoguid,m.AttFileName];
        NSString *downloadurl = [[CommonUtil getDownloadUrl:m.DownLoadUrl]retain];
        NSMutableDictionary *params = self.getPassDictionary;
        [params setObject:savePath forKey:@"savePath"];
        [params setObject:downloadurl forKey:@"url"];
        [params setObject:infoguid forKey:@"folerguid"];
        [self showMBPHUD];
        downloadtaskguid = [[[AttachDownloadTask alloc]initWithContext:self andParams:params]startTask];
    }
}



@end
